#include "ComponentManager.h"

#include "../OgreFramework.h"



ComponentManager::ComponentManager()
{
    //ctor

        m_componentFactory = new ComponentFactory(this);
}

ComponentManager::~ComponentManager()
{
    //dtor
    delete m_componentFactory;
}


/** @brief update
  *
  * @todo: document this function
  */
void ComponentManager::update(double timeSinceLastFrame)
{

    std::map<std::string, Component*>::iterator it;
    for (it=m_components.begin(); it!=m_components.end(); ++it)
    {
        it->second->update(timeSinceLastFrame);
    }
}

/** @brief getComponent
  *
  * @todo: document this function
  */
Component* ComponentManager::getComponent(std::string _name)
{
    if (hasComponent(_name))
    {
        return m_components[_name];
    }
    else
    {
        OgreFramework::getSingletonPtr()->m_pLog->logMessage("ComponentManager::getComponent : component does not exist "+_name );
        return NULL;
    }
}

/** @brief removeComponent
  *
  * @todo: document this function
  */
void ComponentManager::removeComponent(std::string _name)
{
    if (hasComponent(_name))
    {
        m_components[_name]->destroy();
        m_components.erase(_name);
    }
    else
    {
        OgreFramework::getSingletonPtr()->m_pLog->logMessage("ComponentManager::removeComponent : component does not exist "+_name );
    }


}

/** @brief createComponent
  *
  * @todo: document this function
  */
void ComponentManager::createComponent(std::string _name, ComponentArguments _cArgs)
{

    if (!hasComponent(_name))
    {


        Component* comp = m_componentFactory->create( _name, _cArgs);
        if (comp!=0)
        {
//            comp->initialize();
            m_components.insert(std::pair<std::string, Component*>(_name,comp));
        }
        else
        {
            OgreFramework::getSingletonPtr()->m_pLog->logMessage("ComponentManager::createComponent : cannot create component "+_name );
        }
    }
    else
    {
        OgreFramework::getSingletonPtr()->m_pLog->logMessage("ComponentManager::createComponent : component already exist "+_name );
    }

}

/** @brief initializeAllComponents
  *
  * @todo: document this function
  */
void ComponentManager::initializeAllComponents()
{
    std::map<std::string, Component*>::iterator it;
    for (it=m_components.begin(); it!=m_components.end(); ++it)
    {
        it->second->initialize();
    }
}





/** @brief hasComponent
  *
  * @todo: document this function
  */
bool ComponentManager::hasComponent(std::string _name)
{
    if( m_components.count(_name)==0)
        return false;
    else return true;
}


/** @brief loadFromXML
  *
  * @todo: document this function
  */
void ComponentManager::loadFromXML(TiXmlElement* parentElement)
{

}

/** @brief saveToXML
  *
  * @todo: document this function
  */
void ComponentManager::saveToXML(TiXmlElement* parentElement)
{
    std::map<std::string, Component*>::iterator it;
    TiXmlElement* c = new TiXmlElement("Components");
    parentElement->LinkEndChild(c);
    for (it=m_components.begin(); it!=m_components.end(); ++it)
    {
        TiXmlElement* element = new TiXmlElement(it->first.c_str());
        c->LinkEndChild(element);
        it->second->saveToXML(element);
    }

}


